package cn.hospital.seviceimpl;

import cn.hospital.dao.Fo2GroupIdMapper;
import cn.hospital.dao.FoGroupMapper;
import cn.hospital.dao.MeaningUserMapper;
import cn.hospital.dao.PatientInformationSheetMapper;
import cn.hospital.entity.*;
import cn.hospital.service.FoGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FoGroupServiceImpl implements FoGroupService {

    @Resource
    private FoGroupMapper foGroupMapper;

    @Resource
    private PatientInformationSheetMapper patientInformationSheetMapper;

    @Resource
    private Fo2GroupIdMapper fo2GroupIdMapper;

    @Resource
    private MeaningUserMapper meaningUserMapper;


    @Override
    public List<FoGroup> selectFoGroupList(Diagnosis diagnosis, PatientInformationSheet patientInformationSheet, FoGroup foGroup) {
        List<FoGroup> foGroupList = foGroupMapper.selectFoGroupList2(diagnosis, patientInformationSheet, foGroup);
        return foGroupList;
    }
    @Override
    public List<Fo2GroupId> selectFo2GroupList(FoGroup foGroup) {
        List<Fo2GroupId> fo2GroupIdList = fo2GroupIdMapper.selectFo2GroupList2(foGroup);
        return fo2GroupIdList;
    }
    @Override
    public Integer insertGroupPatientInformationSheet(PatientInformationSheet patientInformationSheet, FoGroup foGroup) throws ParseException {
        Integer insert=0;Integer update=0;
        patientInformationSheet.setFoGroupState(1);
        update=patientInformationSheetMapper.updatefoGroupState(patientInformationSheet);
        if(update!=0){
            foGroup.setDateOfEntry(new Date());
            foGroup.setPatientUniquNumber(patientInformationSheet.getPatientId());
            foGroup.setFoState(1);
            insert=foGroupMapper.insertGroup(foGroup);
            return insert;
        }else {
            return update;
        }
    }

    @Override
    public Integer insertGroupMeaningUser(MeaningUser meaningUser, FoGroup foGroup) throws ParseException {
        Integer insert = 0;
        Integer update = 0;
        Fo2GroupId fo2GroupId = new Fo2GroupId();
        meaningUser.setUserState(1);
        update = meaningUserMapper.updatefo2GroupIdState(meaningUser);
        if (update != 0) {
            fo2GroupId.setFo2GroupId(foGroup.getFoGroupId());
            fo2GroupId.setThesetDate(new Date());
            fo2GroupId.setUserId(meaningUser.getUserId());
            fo2GroupId.setFo2GroupState(1);
            insert = fo2GroupIdMapper.insertfo2GroupId(fo2GroupId);
            return insert;
        } else {
            return update;
        }
    }
    @Override
    public List<PatientInformationSheet> selectPatientInformationSheet(FoGroup foGroup) {

        return patientInformationSheetMapper.selectPatientInformationSheet(foGroup);
    }

    @Override
    public Integer deleteFoGroupfoGroupId(Integer[] patientId) {
        Integer delete=0;
        for (Integer patientid : patientId) {
            delete = foGroupMapper.deleteFoGroupfoGroupId(patientid);
            if (delete == 1) {
                PatientInformationSheet patientInformationSheet = new PatientInformationSheet();
                patientInformationSheet.setPatientId(patientid);
                patientInformationSheet.setFoGroupState(2);
                patientInformationSheetMapper.updatefoGroupState(patientInformationSheet);
            }
            if (delete == 0) {
                break;
            }
        }
        return delete;
    }

    @Override
    public Integer deleteFoGroupfo2GroupId(Integer[] userId) {
        Integer delete = 0;
        for (Integer userid : userId) {
            delete = fo2GroupIdMapper.deleteFoGroupfo2GroupId(userid);
            if (delete == 1) {
                MeaningUser meaningUser = new MeaningUser();
                meaningUser.setUserId(userid);
                meaningUser.setUserState(2);
                meaningUserMapper.updatefo2GroupIdState(meaningUser);
            }
            if (delete == 0) {
                break;
            }
        }
        return delete;
    }
    /**
     * 查询随访系统进度管理
     *
     * @return
     */
    public List<FoGroup> queryFoGroup(Map<String, Object> map) {
        return foGroupMapper.selectFoGroup(map);
    }

    /**
     * 查询随访系统进度管理数量
     *
     * @return
     */
    public Long selectFoGroupCount(Map<String, Object> map) {
        return foGroupMapper.selectFoGroupCount(map);
    }

    @Override
    public List<MeaningUser> selectMeaningUser(FoGroup foGroup) {
        List<MeaningUser> meaningUserList = meaningUserMapper.selectDepartmentId(foGroup);
        return meaningUserList;
    }
}
